package controller

import (
	"fx_swift_forex/api"
	"fx_swift_forex/internal/service"
	"fx_swift_forex/pkg/code"
	"fx_swift_forex/pkg/internalerrors"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 发起付款
func CreatePayments(c *gin.Context) {
	req := &api.CreatePaymentsReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err := service.NewPaymentSrv().CreatePayments(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}

// 获取付款人要求
func PayRequirements(c *gin.Context) {
	req := &api.PayRequirementsReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	result, err := service.NewPaymentSrv().PayRequirements(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, result)
	}
}

// 取消、更新付款
func OperatorPayments(c *gin.Context) {
	req := &api.OperatorPaymentReq{}
	if err := c.ShouldBindJSON(&req); err != nil {
		code.Fail(c, http.StatusBadRequest, code.ErrEncodingJSON, err.Error())
		return
	}
	err := service.NewPaymentSrv().OperatorPayment(c, req)
	if err != nil {
		code.Fail(c, http.StatusBadRequest, err.(internalerrors.ServiceError).Code, err.(internalerrors.ServiceError).Msg)
		return
	} else {
		code.Success(c, nil)
	}
}
